﻿/// <summary>
/// Copyright(c) 2008-2009北京玖富时代投资顾问有限公司, All Rights Reserved.
/// 
/// 通用公共方法--XML操作类。考虑到已有可能的扩展所以将XML操作单分出来
/// 
/// 版本：Ver 1.0
/// 作者：【周燕龙】
/// 时间：2009-09-08
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Collections;

namespace Hisuntech.SystemFramework.Common
{
    public class XmlOperate
    {
        public static Hashtable CacheKeyValue = new Hashtable();
        public static string CacheUserProxy = "";

        #region 写

        /// <summary>
        /// 写入信息到XML文件的方法
        /// </summary>
        /// <param name="key">xml的key值</param>
        /// <param name="key_value">xml的value值</param>
        public static void WriteXml(string key, string key_value)
        {
            WriteXml(key, key_value, "APP.CONFIG");
            //邓太华　２００８．１０．２８
            ClearCache();
        }

        /// <summary>
        /// 写入信息到XML文件的方法
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="key_value">value值</param>
        /// <param name="FileName">文件地址</param>
        public static void WriteXml(string key, string key_value, string FileName)
        {
            //实例化一个XML文档
            XmlDocument dc = new XmlDocument();
            //			string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".config";
            string filePath = Application.StartupPath + "\\" + FileName;
            File.SetAttributes(filePath, FileAttributes.Normal);
            dc.Load(filePath);//加载XML配置文件


            //查找XML文件的节点
            XmlNode node = dc.SelectSingleNode("//appSettings");
            if (node == null)
            {
                throw new InvalidOperationException("appSettings段在配置文件中没有找到.");
            }
            try
            {
                //从节点中得到KEY这个元素
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                if (elem != null)
                {
                    //设置属性value的值
                    elem.SetAttribute("value", key_value);
                }
                else
                {
                    //创建一个新的元素
                    elem = dc.CreateElement("add");
                    elem.SetAttribute("key", key);
                    elem.SetAttribute("value", key_value);
                    node.AppendChild(elem);
                }
                //保存更改的配置文件.
                dc.Save(filePath);
            }
            catch
            {
                throw;
            }

        }
        
        #endregion

        #region 读

        /// <summary>
        /// 读取XML文件的方法
        /// </summary>
        /// <param name="key">XML的key值</param>
        /// <returns>XML的value值</returns>
        public static string ReadXml(string key)
        {
            //下面是原始代码
            //return ReadXml(key, "APP.CONFIG");
            //
            if (!CacheKeyValue.ContainsKey(key))
                CacheKeyValue.Add(key, ReadXml(key, "APP.CONFIG"));
            else if (CacheKeyValue[key] == null)
                CacheKeyValue[key] = ReadXml(key, "APP.CONFIG");
            return CacheKeyValue[key].ToString();
        }

        /// <summary>
        /// 读取XML文件的方法
        /// </summary>
        /// <param name="key">XML的key值</param>
        /// <param name="FileName">xml文件地址</param>
        /// <returns>XML的value值</returns>
        public static string ReadXml(string key, string FileName)
        {
            string returnValue = "";
            try
            {
                //实例化一个XML文档
                XmlDocument dc = new XmlDocument();
                string filePath = Application.StartupPath + "\\" + FileName;
                dc.Load(filePath);//加载XML配置文件
                //查找XML文件的节点
                XmlNode node = dc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new InvalidOperationException("appSettings段在配置文件中没有找到.");
                }
                try
                {
                    //从节点中得到KEY这个元素
                    XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                    if (elem != null)
                    {
                        returnValue = elem.GetAttribute("value").ToString();
                    }
                }
                catch
                {
                    throw;
                }

            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            return returnValue;
        }
        
        #endregion       


        /// <summary>
        /// 清除缓存
        /// </summary>
        public static void ClearCache()
        {
            CacheKeyValue.Clear();
            CacheUserProxy = "";
        }
    }
}
